package com.wkbb.sports.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wkbb.sports.dto.AppVersionDTO;
import com.wkbb.sports.mapper.HlifeAppVersionMapper;
import com.wkbb.sports.model.HlifeAppVersion;
import com.wkbb.sports.service.AppService;

@Service
public class AppServiceImpl implements AppService {
	
	@Autowired
	private HlifeAppVersionMapper appVersionMapper;

	@Override
	public AppVersionDTO getLastVersion(Byte osKind) throws Exception {
		Map<String, Object> params = new HashMap<>();
		params.put("osKind", osKind);
		HlifeAppVersion ver = appVersionMapper.getLastVersion(params);
		if(ver == null) {
			throw new RuntimeException("can not find last version for osKind:" + osKind);
		}
		AppVersionDTO dto = new AppVersionDTO();
		dto.setId(ver.getId());
		dto.setOsKind(ver.getOsKind());
		dto.setDownloadPath(ver.getDownloadPath());
		dto.setVersion(ver.getVersion());
		dto.setVersionId(ver.getVersionId());
		dto.setDescription(ver.getDescription());
		dto.setCreateTime(ver.getCreateTime());
		return dto;
	}

	@Override
	public void addAppVersion(AppVersionDTO appVersion) throws Exception {
		HlifeAppVersion version = new HlifeAppVersion();
		version.setOsKind(appVersion.getOsKind());
		version.setVersion(appVersion.getVersion());
		version.setVersionId(appVersion.getVersionId());
		version.setDownloadPath(appVersion.getDownloadPath());
		version.setDescription(appVersion.getDescription());
		appVersionMapper.insert(version);
	}

}
